<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：UserModel.php
 * ----------------------------------------------------------------------------
 * 功能描述：ECTouch 用户模型
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 * ----------------------------------------------------------------------------
 */

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class MshopModel extends BaseModel {

    protected $table = 'mshop';
  function getIdByUser_id($user_id) {

     $sql = " SELECT mshop_id FROM " . $this->pre ."users WHERE user_id= '$user_id'";
     $data=$this->row($sql);

     return $data['mshop_id'];
    }
    //判断是否存在上级推荐人，存在表示是在上级那边购买，否则不显示ID
    function getPidByMshop_id($mshop_id) {

     $sql = " SELECT mshop_id FROM " . $this->pre ."users WHERE user_id= '$_SESSION[user_id]'";
     $data=$this->row($sql);

    
     if($data['mshop_id']==$mshop_id)
     {
        $sql = " SELECT mshop_pid FROM " . $this->pre ."mshop WHERE mshop_id= '$mshop_id'";
        $data2=$this->row($sql);
        if($data2['mshop_pid'])
        {
          $mshop_id=$data2['mshop_pid'];
        }
     }
     else
     {
        $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_id= '$mshop_id'";
        $data2=$this->row($sql);
        if(!$data2['mshop_id'])
        {
          $mshop_id=0;
        }
     }
     return $mshop_id;
    }
  function get_info($mshop_id) {

     $sql = " SELECT m.*,u.user_name,u.mobile_phone FROM " 
            . $this->pre ."mshop as m "
            ." left join " .$this->pre ."users as u on u.mshop_id=m.mshop_id "
            ." WHERE m.mshop_id= '$mshop_id'";
     return $this->row($sql);
    }
  function get_userinfo($mshop_id) {

     $sql = " SELECT user_id,user_name,email,mobile_phone FROM " . $this->pre ."users WHERE mshop_id= '$mshop_id'";

     return $this->row($sql);
    }
  
  //计算所有子店铺数量
  function count_child_mshop($mshop_id)
  {
    $num=0;
    $sql = " SELECT m.mshop_id FROM " . $this->pre ."mshop as m  ".
        " left join ".$this->pre."users as u on u.mshop_id=m.mshop_id  WHERE u.mshop_id!=0 and  m.mshop_pid= '$mshop_id'";
    $fir=$this->query($sql);
    foreach ($fir as $key => $value) {
          $num++;
          $sql = " SELECT  count(*) as num FROM " . $this->pre ."mshop as m  ".
           " left join ". $this->pre."users as u on u.mshop_id=m.mshop_id WHERE  u.mshop_id!=0 and m.mshop_pid= '$value[mshop_id]'";
          $sc_num=$this->row($sql);
          $num=$num+$sc_num['num'];          
    }
     return $num;
  }
  //计算不同子店铺数量
  function count_difchild_mshop($mshop_id)
  {
    $arNum=array();
    $num=0;
    $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_pid= '$mshop_id'";
    $fir=$this->query($sql);
    $arNum[0]=count($fir);
    foreach ($fir as $key => $value) {
         $sql = " SELECT count(*) as num FROM " . $this->pre ."mshop WHERE mshop_pid= '$value[mshop_id]'";
         $sc_num=$this->row($sql);
         $num=$num+$sc_num['num'];
    }
    $arNum[1]=$num;
     return $arNum;
  }
  //获取该店铺包括自己的所有子店铺
  function get_child_mshop($mshop_id)
  {
    $mshop_ar=array();
    $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_pid= '$mshop_id'";
    $sec=$this->query($sql);
    $mshop_ar['fir']=$mshop_id;
    
    foreach ($sec as $key => &$value) {
         $num++;
         $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_pid= '$value[mshop_id]'";
         $thr=$this->query($sql);
         if(count($thr))
         {
           $length=count($mshop_ar['thr']);
           $value['child']=$thr;   
         }


    }
    $mshop_ar['sec']=$sec;
     return $mshop_ar;
  }
  //查看店铺及其子店铺的销售情况
  function get_order_sales($mshop_id)
  {
    $sals=array();
    $sals['fir']=$this->order_sales_byid($mshop_id);

    $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_pid= '$mshop_id'";
    $fir=$this->query($sql);
    $sals['fir']['sec_num']=count($fir);
    $sals['fir']['thr_num']=0;
    $sals['fir']['fth_num']=0;

    foreach ($fir as $key => $value) {
       $sec=$this->order_sales_byid($value['mshop_id']);
       
       $sals['sec']['nopaid_num']=$sals['sec']['nopaid_num']?($sals['sec']['nopaid_num']+$sec['nopaid_num']):$sec['nopaid_num'];
       $sals['sec']['nopaid_amount']=$sals['sec']['nopaid_amount']?($sals['sec']['nopaid_amount']+$sec['nopaid_amount']):$sec['nopaid_amount'];
       $sals['sec']['end_num']=$sals['sec']['end_num']?($sals['sec']['end_num']+$sec['end_num']):$sec['end_num'];
       $sals['sec']['end_amount']=$sals['sec']['end_amount']?($sals['sec']['end_amount']+$sec['end_amount']):$sec['end_amount'];
       $sals['sec']['return_num']=$sals['sec']['return_num']?($sals['sec']['return_num']+$sec['return_num']):$sec['return_num'];
       $sals['sec']['return_amount']=$sals['sec']['return_amount']?$sals['sec']['return_amount']+$sec['return_amount']:$sec['return_amount'];

       $sql = " SELECT mshop_id FROM " . $this->pre ."mshop WHERE mshop_pid= '$value[mshop_id]'";
       $sec_data=$this->query($sql);

       $sals['fir']['thr_num']=$sals['fir']['thr_num']+count($sec_data);
       foreach ($sec_data as $key2 => $value2) {
         $thr=$this->order_sales_byid($value2['mshop_id']);
       
         $sals['thr']['nopaid_num']=$sals['thr']['nopaid_num']?($sals['thr']['nopaid_num']+$thr['nopaid_num']):$thr['nopaid_num'];
         $sals['thr']['nopaid_amount']=$sals['thr']['nopaid_amount']?($sals['thr']['nopaid_amount']+$thr['nopaid_amount']):$thr['nopaid_amount'];
         $sals['thr']['end_num']=$sals['thr']['end_num']?($sals['thr']['end_num']+$thr['end_num']):$thr['end_num'];
         $sals['thr']['end_amount']=$sals['thr']['end_amount']?($sals['thr']['end_amount']+$thr['end_amount']):$thr['end_amount'];
         $sals['thr']['return_num']=$sals['thr']['return_num']?($sals['thr']['return_num']+$thr['return_num']):$thr['return_num'];
         $sals['thr']['return_amount']=$sals['thr']['return_amount']?($sals['thr']['return_amount']+$thr['return_amount']):$thr['return_amount'];
       }
    }
    return $sals;

  }
  //查看店铺的销售情况
  function order_sales_byid($mshop_id)
  {
    //未付款，已完成，退货的订单数和金额
    $sql="select count(*) as num,sum(order_amount) as s_amount from "
          .$this->pre ."order_info WHERE mshop_id= '$mshop_id' and pay_status!=2";
    $fir1=$this->row($sql);

    $sql="select count(*) as num,sum(money_paid) as s_amount from "
          .$this->pre ."order_info WHERE mshop_id= '$mshop_id' and pay_status=2 ";
    $fir2=$this->row($sql);

    $sql="select count(*) as num,sum(order_amount) as s_amount from "
          .$this->pre ."order_info WHERE mshop_id= '$mshop_id' and order_status=4";
    $fir3=$this->row($sql);

    $sals['nopaid_num']=$fir1['num']?$fir1['num']:0;
    $sals['nopaid_amount']=$fir1['s_amount']?$fir1['s_amount']:0;
    $sals['end_num']=$fir2['num']?$fir2['num']:0;
    $sals['end_amount']=$fir2['s_amount']?$fir2['s_amount']:0;
    $sals['return_num']=$fir3['num']?$fir3['num']:0;
    $sals['return_amount']=$fir3['s_amount']?$fir3['s_amount']:0;

    return $sals;
  }
    //获取该店铺包括所有父店铺--因为只有三级所以不用递归求值
  function get_parent_mshop($mshop_id)
  {
    $mshop_all=array();
    $sql = " SELECT mshop_pid FROM " . $this->pre ."mshop WHERE mshop_id= '$mshop_id'";
    $fir=$this->row($sql);
    $mshop_all[0]=$mshop_id;
    if($fir['mshop_pid'])
    {
      $mshop_all[1]=$fir['mshop_pid'];
      $sql = " SELECT mshop_pid FROM " . $this->pre ."mshop WHERE mshop_id= '$fir[mshop_pid]'";
      $sec=$this->row($sql);
      if($sec['mshop_pid'])
  	  {
  		  $mshop_all[2]=$sec['mshop_pid'];
  		  // $sql = " SELECT mshop_pid FROM " .  $this->pre ."mshop WHERE mshop_id= '$sec[mshop_pid]'";
  		  // $thr=$this->row($sql);
  		  // if($thr['mshop_pid'])
  		  // $mshop_all[3]=$thr['mshop_pid'];  
  	  }
     
    }

     return $mshop_all;
  }

   //佣金的累加或消减
   function deal_profit($order_sn,$mshop_id,$amount,$shipping_fee=0,$add=0) 
   {
     //自己在自己的店铺购买 没有佣金
      $sql = " SELECT mshop_id FROM " .  $this->pre ."users WHERE user_id= '$_SESSION[user_id]'";
      $mshopinfo=$this->row($sql);
      if( $mshopinfo['mshop_id']==$mshop_id)
      {
        return false;
      }
      else
      {
		      $k_amount=$amount-$shipping_fee;//计算佣金的营业额
          $ids=$this->get_parent_mshop($mshop_id);

          $result=false;
          $profit=0;
          $now_time=time();            
          // $sql=" select * from ".$this->pre ."mshop_frozen_profit WHERE status!=2 and order_sn= '$order_sn' ";
          // $data_frozen=$this->row($sql);
          if($k_amount>0)
          {
             foreach ($ids as $key => $value) 
             {
              switch ($key) {
                case 0:{
                  $profit=floatval($k_amount)*C('mshop_rate');
                 if($add)
                  { 
				            $sql_infr = " insert into   " . $this->pre ."mshop_frozen_profit(`mshop_id`,`order_sn`,`goods_price`,`frozen_profit`,`ctime`,`status`,`myshop`) values('$mshop_id','$order_sn','$k_amount','$profit','$now_time','0','1')";
                            $insert_fr=$this->query($sql_infr);
        				    $sql = " update  " . $this->pre ."mshop set sum_amount=sum_amount+$amount,self_sum_amount=self_sum_amount+$amount,sum_profit=(sum_profit+$profit) WHERE mshop_id= '$value'";
        				 }
                 else
                 {
                     $sql = " update  " . $this->pre ."mshop set  sum_amount=sum_amount-$amount,self_sum_amount=self_sum_amount-$amount,sum_profit=(sum_profit-$profit) WHERE mshop_id= '$value'";

                 }
        
                  break;
                }

                case 1:
                {
                  $profit=floatval($k_amount)*C('first_rate');
                   if($add)
                  {
					          $sql_infr = " insert into   " . $this->pre ."mshop_frozen_profit(`mshop_id`,`order_sn`,`goods_price`,`frozen_profit`,`ctime`,`status`) values('$value','$order_sn','$k_amount','$profit','$now_time','0')";
                    $insert_fr=$this->query($sql_infr);
      					    $sql = " update  " . $this->pre ."mshop set sum_amount=sum_amount+$amount,sum_profit=(sum_profit+$profit) WHERE mshop_id= '$value'";
      				   }
                 else
                 {
                   $sql = " update  " . $this->pre ."mshop set  sum_amount=sum_amount-$amount,sum_profit=(sum_profit-$profit) WHERE mshop_id= '$value'";
  
                 }
           
        
                  break;
                }

               case 2:
               {
                 $profit=floatval($k_amount)*C('second_rate');
                  if($add)
                  { 
				              $sql_infr = " insert into   " . $this->pre ."mshop_frozen_profit(`mshop_id`,`order_sn`,`goods_price`,`frozen_profit`,`ctime`,`status`) values('$value','$order_sn','$k_amount','$profit','$now_time','0')";
                               $insert_fr=$this->query($sql_infr);
				               $sql = " update  " . $this->pre ."mshop set sum_amount=sum_amount+$amount,sum_profit=(sum_profit+$profit) WHERE mshop_id= '$value'";
				          }
                 else
                 {
                     $sql = " update  " . $this->pre ."mshop set  sum_amount=sum_amount-$amount,sum_profit=(sum_profit-$profit) WHERE mshop_id= '$value'";
                 }
        
                  break;
               }
              }
              $result=$this->query($sql);

            }
           
            
             
          }
        
         if(!$add)
        {
          $sql_pro="update  ".$this->pre ."mshop_frozen_profit  set status=2 where status!=2 and  order_sn='$order_sn'";
          $result_pro=$this->query($sql_pro);
        }
         return $result;
      }

    

     
    }


  



}
